package com.ald.crossex.action.wechat;

import com.ald.crossex.dto.baidu.BDMapRouteMatrixRequest;
import com.ald.crossex.dto.baidu.BDMapRouteMatrixResponse;
import com.ald.crossex.dto.wechat.*;
import com.ald.crossex.entity.TCeCoreSystem;
import com.ald.crossex.entity.TCeCoreUser;
import com.ald.crossex.entity.TCeCoreWechat;
import com.ald.crossex.service.GoodsService;
import com.ald.crossex.service.SystemService;
import com.ald.crossex.service.UserService;
import com.ald.crossex.service.wechat.DeliveryService;
import com.ald.crossex.service.wechat.WechatService;
import com.ald.crossex.system.SystemConfig;
import com.ald.crossex.system.SystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lishijiong on 2015-01-20.
 */
@Controller
@RequestMapping(value = "/")
public class DeliveryController extends BaseController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private DeliveryService deliveryService;
    @Autowired
    private WechatService wechatService;
    @Autowired
    private UserService userService;
    @Autowired
    private SystemService systemService;

    @RequestMapping(value = "delivery")
    public ModelAndView redirectDelivery(@RequestParam(value = "code") String code, @RequestParam(value = "state") String state, HttpServletRequest request) throws Exception {

        TCeCoreWechat wechat = webAuthorize(wechatService, code, state);

        ModelAndView mav = new ModelAndView("/delivery");
        mav.addObject("types", goodsService.listGoodsType());
        if(wechat!=null){
            mav.addObject("wid",wechat.getWid());
        }
        TCeCoreUser user = userService.getUserByWechatId(wechat.getWid());
        if(user != null){
            mav.addObject("uid",user.getUid());
        }
        generateJsSDKConfig(request, mav, code, state);

        return mav;
    }


    //@RequestMapping(value = "delivery")
    public ModelAndView redirectDelivery(){
        ModelAndView mav = new ModelAndView("/delivery");
        mav.addObject("types", goodsService.listGoodsType());
        return mav;
    }

    /**
     * 计算费用和时间
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "calFee", method = RequestMethod.POST)
    @ResponseBody
    public CalculateFeeResponse calculateFee(@RequestBody CalculateFeeRequest request) throws Exception {

        CalculateFeeResponse response = new CalculateFeeResponse();
        BDMapRouteMatrixRequest request1 = new BDMapRouteMatrixRequest();
        request1.setOrigins(request.getSendPlace());
        request1.setDestinations(request.getReceivePlace());
        request1.setAk(SystemConfig.getVal("baidu_map_ak"));
        request1.setTactics(11);
        request1.setMode("driving");
        BDMapRouteMatrixResponse rsp = calculateDistance(request1);

        Integer deliverySeconds = rsp.getResult().getElements().get(0).getDuration().getValue();
        Integer deliveryDistance = rsp.getResult().getElements().get(0).getDistance().getValue();

        TCeCoreSystem unitPrice = systemService.getSystemByKey(SystemConstant.SYSTEM_DELIVERY_UNIT_PRICE);
        TCeCoreSystem preAdd = systemService.getSystemByKey(SystemConstant.SYSTEM_DELIVERY_PRE_ADD_SECONDS);
        TCeCoreSystem sufAdd = systemService.getSystemByKey(SystemConstant.SYSTEM_DELIVERY_SUF_ADD_SECONDS);

        Float price = Float.parseFloat(unitPrice.getValue());
        Integer preAddSeconds = Integer.parseInt(preAdd.getValue());
        Integer sufAddSeconds = Integer.parseInt(sufAdd.getValue());

        response.setSendFee((deliveryDistance / 1000) * price);
        response.setSendTime(preAddSeconds + deliverySeconds + sufAddSeconds);
        return response;

    }

    @RequestMapping(value = "publish", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse publishDelivery(@RequestBody PublishDeliveryRequest request) throws Exception {

        BaseResponse response = deliveryService.publishGoods(request);

        return response;
    }


}
